home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT51.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  7.7 KB  |  273 lines

  1. /*
  2. ==============================================================================
  3.               WordUp Graphics Toolkit Version 5.0                     
  4.                  Demonstration Program 51                         
  5.                                           
  6.  Demonstrates the scan converted polygon routines, and shadebob techniques.  
  7.                                           
  8.  *** PROJECT ***                                                             
  9.  This program requires the WGT5_WC.LIB file to be linked.                    
  10.                                           
  11.  *** DATA FILES ***                                                          
  12.  SQUARE.PLY,  STAR.PLY (Autodesk Animator Polygon files)                     
  13.                                WATCOM C++ VERSION 
  14. ==============================================================================
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <conio.h>
  20. #include <math.h>
  21. #include <time.h>
  22. #include <wgt5.h>
  23.  
  24. /* "Polybob" example:
  25.  
  26.    A common part in a lot of computer demos involves a technique called
  27.    shadebobs.   They simply mean that each pixel drawn is based on the
  28.    one that previously existed at that spot.  Usually the color of each
  29.    pixel is increased or decreased by 1.  Using a palette with many
  30.    shades of colors, this can produces a colorful display on your monitor.
  31.  
  32.    Polybobs are an extension of this idea.  Most shadebobs are simple shapes
  33.    like squares or circles.  Using WGT's polygon routines, we can write
  34.    our own horizontal line routine which will do the shadebobs for us.
  35.    Using this, we can do shadebobs with complex polygons.
  36.  
  37.    By using rendered polygons, once the shape is rendered, all it needs to
  38.    do is draw a list of horizontal lines, which is very fast.  Of course, 
  39.    this means our shape will not be able to change, or rotate.  The second
  40.    part of this demo will show you another technique using the wsolidpoly
  41.    routines to draw a see-through polygon with another shadebob method.
  42.  
  43.    */
  44.  
  45.  
  46. color pal[256];
  47. short oldmode;
  48.  
  49. tpolypoint pts[150];      /* Stores points of polygon. */
  50. short nump;               /* Number of points in this polygon. */
  51.  
  52. unsigned char shadebob_increment;  /* number of colors to add to each */
  53.           /* pixel when drawing an object with the shadebob technique. */
  54. block virt;   /* A virtual screen */
  55.  
  56.  
  57. void setup_palette (void)
  58. /* Sets up a nice looking palette for shadebobs */
  59. {
  60.   short i;
  61.  
  62.   for (i = 0; i < 256; i++)
  63.     wsetrgb (i, 64 * sin(i * 0.017), 64 * sin(i * 0.102), 64 * sin(i * 0.008), pal);
  64.   wsetpalette (0, 255, pal);
  65. }
  66.  
  67.  
  68. void setup_transparent_palette (void)
  69. /* Sets up a palette used for transparent images. */
  70. {
  71.   short i;
  72.  
  73.   for (i = 0; i < 128; i++)
  74.     wsetrgb (i, 64 * sin(i * 0.017), 64 * sin(i * 0.02), 64 * sin(i * 0.01), pal);
  75.   for (i = 128; i < 256; i++)
  76.   {
  77.     pal[i].b = pal[i - 128].r;   /* Here we will access the palette */
  78.     pal[i].g = 0;
  79.     pal[i].r = 0;
  80.   }                               
  81.                   
  82.   wsetpalette (0, 255, pal);
  83. }
  84.  
  85.  
  86. void load_poly (char *polyfilename)
  87. /* Loads an Autodesk Animator Polygon definition file. */
  88. {
  89.   FILE *polyfile;
  90.   short magic, i;
  91.  
  92.   polyfile = fopen (polyfilename, "rb");
  93.   if (polyfile == NULL) 
  94.     return;
  95.   fread (&nump, 2, 1, polyfile);
  96.   fread (&magic, 2, 1, polyfile);  /* unused space  4 bytes */
  97.   fread (&magic, 2, 1, polyfile);
  98.   fread (&magic, 2, 1, polyfile);  /* close shape flag, and magic number (0x99) */
  99.   /* Assume we always want a closed shape for filled polygons. */
  100.  
  101.   for (i = 0; i < nump; i++)
  102.   {
  103.     fread (&pts[i].x, 2, 1, polyfile);
  104.     fread (&pts[i].y, 2, 1, polyfile);
  105.     fread (&magic, 2, 1, polyfile); /* Z coord is always 0, discard */
  106.   }
  107.   fclose (polyfile);
  108. }
  109.  
  110.  
  111. void shadebobline (short x, short x2, short y)
  112. {
  113.   short swap;       /* Used for sorting x coords */
  114.   short length;     /* Horizontal line length (in pixels) */
  115.   block temp;       /* unsigned char far * to the video screen */
  116.  
  117.   if (x2 < x)
  118.   {
  119.     swap = x2;
  120.     x2 = x;
  121.     x = swap;
  122.   }
  123.  
  124.   if (x < tx) 
  125.     x = tx;    /* Clip the left edge */
  126.   if (x2 > bx) 
  127.     x2 = bx;  /* Clip the right edge */
  128.  
  129.   length = x2 - x + 1;  /* Find the length of the line */
  130.  
  131.   if (length > 0)
  132.   {
  133.     temp = abuf + y * 320 + x;  /* Makes a pointer to the first point on */
  134.                 /* the line.  Abuf is the current working */
  135.                 /* video page, and we add the number of */
  136.                 /* pixels up to the point. */
  137.  
  138.     while (length > 0)          /* Draw each pixel in the line */
  139.     {
  140.       *temp = *temp + shadebob_increment; /* add to the color */
  141.       temp++;                             /* go to the next pixel */
  142.       length--;                           /* decrease loop counter */
  143.     }
  144.   }
  145. }
  146.  
  147.  
  148. void do_polybob (void)
  149. /* Displays the moving polybob on the screen until you hit a key. */
  150. {
  151.   wscanpoly polybob; /* The rendered polygon */
  152.   short xdir = 1;
  153.   short ydir = 1;
  154.   short xcoord, ycoord;
  155.  
  156.   xcoord = rand () % 280;
  157.   ycoord = rand () % 160;
  158.  
  159.   wscan_convertpoly (pts, nump, &polybob);
  160.   wcls (0);
  161.   
  162.   do {
  163.     xcoord += xdir;  /* Move the polybob */
  164.     ycoord += ydir;
  165.  
  166.     if (xcoord < 0)               /* Make the polybob switch directions */
  167.       xdir = rand() % 4;          /* when it hits the side of the screen. */
  168.     else if (xcoord > 280) 
  169.       xdir = -rand() % 4;
  170.     if (ycoord < 0) 
  171.       ydir = rand() % 4;
  172.     else if (ycoord > 160) 
  173.       ydir = -rand() % 4;
  174.  
  175.     wdraw_scanpoly (xcoord, ycoord, &polybob, shadebobline);
  176.   } while (!kbhit ());
  177.  
  178.  wfree_scanpoly (&polybob);
  179.  
  180.  while (kbhit ()) 
  181.    getch ();  /* Clear out keyboard buffer */
  182.  
  183.  
  184. void do_transparentbob (void)
  185. /* Displays the moving polybob on the screen until you hit a key. */
  186. {
  187.   wscanpoly polybob; /* The rendered polygon */
  188.   short xdir = 1, ydir = 1;
  189.   short xcoord, ycoord;
  190.  
  191.   xcoord = rand () % 280;
  192.   ycoord = rand () % 160;
  193.  
  194.   wscan_convertpoly (pts, nump, &polybob);
  195.  
  196.   do {
  197.    xcoord += xdir;  /* Move the polybob */
  198.    ycoord += ydir;
  199.  
  200.    if (xcoord < 0)              /* Make the polybob switch directions */
  201.      xdir = rand() % 4;         /* when it hits the side of the screen. */
  202.    else if (xcoord > 280) 
  203.      xdir = -rand() % 4;
  204.    if (ycoord < 0) 
  205.      ydir = rand() % 4;
  206.    else if (ycoord > 160) 
  207.      ydir = -rand() % 4;
  208.  
  209.    wdraw_scanpoly (xcoord, ycoord, &polybob, shadebobline);
  210.    wretrace ();
  211.    wdraw_scanpoly (xcoord, ycoord, &polybob, shadebobline);
  212.   } while (!kbhit ());
  213.  
  214.   wfree_scanpoly (&polybob);
  215.  
  216.   while (kbhit ()) 
  217.     getch ();  /* Clear out keyboard buffer */
  218.  
  219.  
  220. void draw_transparent_background (void)
  221. {
  222.   short x, y;
  223.  
  224.   wcls (0);
  225.   for (y = 0; y < 200; y++)
  226.     for (x = 0; x < 320; x++)
  227.     {
  228.       wsetcolor (y % 128);
  229.       wfastputpixel (x, y);
  230.     }
  231.  
  232.   wtexttransparent (TEXTFG);
  233.   wtextcolor (13);
  234.   wgtprintf (10, 50, NULL, "This is a see-through polygon");
  235.   wgtprintf (10, 90, NULL, "This is a see-through polygon");
  236.   wgtprintf (10, 130, NULL, "This is a see-through polygon");
  237.   wgtprintf (10, 170, NULL, "This is a see-through polygon");
  238. }
  239.  
  240.  
  241. void main(void)
  242. {
  243.   oldmode = wgetmode ();
  244.   printf ("WGT Example #51\n\n");
  245.   printf ("Polygon routines are used in conjunction with a custom WHLINE so that\n");
  246.   printf ("see-through images can be created. Press a key to advance each section.\n");
  247.   printf ("\n\nPress any key to continue.\n");
  248.   getch();
  249.  
  250.   vga256 ();
  251.   winitpoly (WGT_SYS.yres);
  252.  
  253.   setup_palette ();
  254.   shadebob_increment = 1;
  255.   load_poly ("square.ply");
  256.   do_polybob ();
  257.   load_poly ("star.ply");
  258.   do_polybob ();
  259.  
  260.   setup_transparent_palette ();
  261.   shadebob_increment = 128;
  262.   draw_transparent_background ();
  263.   virt = wnewblock (0, 0, 319, 199);
  264.   do_transparentbob ();
  265.  
  266.   wfreeblock (virt); 
  267.   wdeinitpoly ();
  268.   wsetmode (oldmode);
  269. }
  270.  
  271.